// pages/category/category.js
const util=require('../../utils/util')
const api=require('../../config/api')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        categoryList:[],
        currentCategory:{},
        currentSubCategoryList:{},
        goodsCount:0,
        activeKey:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getCatalog();
    },
    getCatalog(){
        let that=this;
        wx.showLoading({
          title: '加载中...',
        });
        util.request(api.CatalogList).then(function(res){
            that.setData({
                categoryList:res.data.categoryList,
        currentCategory:res.data.currentCategory,
        currentSubCategoryList:res.data.currentSubCategory,
            });
            wx.hideLoading();
        });
        util.request(api.GoodsCount).then(function(res){
            that.setData({
                goodsCount:res.data.goodsCount
            })
        })
    },
    onChange(e){
        // console.log(e);
        let newkey=e.detail
        let currentId=this.data.categoryList[newkey].id
        // console.log(currentId);
        this.setData({
            activeKey:e.detail,
        })
        this.getCurrentCategory(currentId)
    },
    getCurrentCategory(id){
        let that=this;
        util.request(api.CatalogCurrent,{id:id}).then(res=>{
            // console.log(res);
         if(res.errno==0){
            that.setData({
                currentCategory:res.data.currentCategory,
        currentSubCategoryList:res.data.currentSubCategory,
            })
         }
        })
    }

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
   
})